import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:shishi/Colors.dart';
import 'package:shishi/Tab/home.dart';
import 'package:shishi/bean/touxiang.dart';
import 'package:shared_preferences/shared_preferences.dart';

const TextStyle boldText = TextStyle(
  fontWeight: FontWeight.bold,
);
class Login extends StatefulWidget {
  static final String path = "lib/src/pages/invitation/inauth.dart";
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  bool signupForm;

  @override
  void initState() {
    super.initState();
    signupForm = true;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height * 0.4,
            decoration: BoxDecoration(
              color: Cos.zhutise1,
              borderRadius: BorderRadius.only(bottomRight: Radius.circular(50),bottomLeft: Radius.circular(50))
            ),
          ),
          Container(
            alignment: Alignment.topCenter,
            margin: EdgeInsets.only(top: 20),
            child:
                Column(
                  children: [
                    Text('曦诗',style: TextStyle(
                        fontFamily: 'lixuke',
                      fontSize: 30,
                      color: Cos.zhutise
                    ),),
                  ],
                ),
          ),
          SingleChildScrollView(
            padding: const EdgeInsets.all(16.0),
            child: Container(
              margin: EdgeInsets.only(top: MediaQuery.of(context).size.height * 0.2),
              child: Column(
                children: <Widget>[
                  const SizedBox(height: kToolbarHeight - 16.0),
                  const SizedBox(height: 20.0),
                  Container(
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10.0),
                        boxShadow: [
                          BoxShadow(
                            color: Cos.zhutise1,
                            offset: Offset(0, 5),
                            blurRadius: 10.0,
                          )
                        ]),
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Container(
                          width: double.infinity,
                          decoration: BoxDecoration(
                            color: Colors.grey.shade100,
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(10.0),
                              topRight: Radius.circular(10.0),
                            ),
                          ),
                          child: ToggleButtons(
                            renderBorder: false,
                            selectedColor: Cos.zhutise,
                            fillColor: Colors.transparent,
                            children: <Widget>[
                              Padding(
                                padding: const EdgeInsets.all(16.0),
                                child: Text(
                                  "登陆",
                                  style: boldText,
                                ),
                              ),
                              Padding(
                                padding: const EdgeInsets.all(8.0),
                                child: Text(
                                  "注册",
                                  style: boldText,
                                ),
                              ),
                            ],
                            isSelected: [signupForm, !signupForm],
                            onPressed: (index) {
                              setState(() {
                                signupForm = index == 0;
                              });
                            },
                          ),
                        ),
                        AnimatedSwitcher(
                          duration: Duration(milliseconds: 200,),
                          child: signupForm ? SignUp() : SignIn(),
                        ),
                      ],
                    ),
                  ),
                  const SizedBox(height: 20.0),

                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class SignUp extends StatelessWidget {


  SharedPreferences sharedPreferences;
  String name;
  String phone;

  _sava() async {
    sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setString("name", name);
    sharedPreferences.setString("phone", phone);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        children: <Widget>[
          TextField(
            onChanged: (v) {
              name = v;
            },
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              contentPadding: const EdgeInsets.all(
                16.0,
              ),
              hintText: "输入你的姓名",
            ),
          ),
          const SizedBox(height: 16.0),
          TextField(
            onChanged: (v) {
              phone = v;
            },
            keyboardType: TextInputType.phone,
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              hintText: "请输入手机号",
              contentPadding: const EdgeInsets.all(
                16.0,
              ),
              prefixStyle: boldText.copyWith(color: Colors.black),
            ),
          ),
          const SizedBox(height: 16.0),
          Container(
            width: double.infinity,
            padding: const EdgeInsets.symmetric(
              horizontal: 32.0,
            ),
            child: RaisedButton(
              elevation: 0,
              shape: StadiumBorder(),
              highlightElevation: 0,
              textColor: Colors.white,
              color: Cos.zhutise,
              onPressed: (){
                _sava();
                Navigator.of(context).pushAndRemoveUntil(
                    CupertinoPageRoute(builder: (context) => Home()),
                        (route) => route == null);
              },
              child: Text("登陆"),

            ),
          ),
          const SizedBox(height: 10.0),
        ],
      ),
    );
  }
}

class SignIn extends StatelessWidget {

  SharedPreferences sharedPreferences;
  String _name;
  String _phone;
  String _password;

  _savas() async {
    sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setString("_name", _name);
    sharedPreferences.setString("_phone", _phone);
    sharedPreferences.setString("_password", _password);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        children: <Widget>[
          TextField(
            onChanged: (name) {
              _name = name;
            },
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              contentPadding: const EdgeInsets.all(
                16.0,
              ),
              hintText: "输入你的姓名",
            ),
          ),
          const SizedBox(height: 16.0),
          TextField(
            onChanged: (one) {
              _phone = one;
            },
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              contentPadding: const EdgeInsets.all(
                16.0,
              ),
              hintText: "输入您的手机号",
            ),
          ),
          const SizedBox(height: 16.0),
          TextField(
            onChanged: (ps) {
              _password = ps;
            },
            obscureText: true,
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              hintText: "密码",
              contentPadding: const EdgeInsets.all(
                16.0,
              ),
              prefixStyle: boldText.copyWith(color: Colors.black),
            ),
          ),
          const SizedBox(height: 16.0),
          Container(
            width: double.infinity,
            padding: const EdgeInsets.symmetric(
              horizontal: 32.0,
            ),
            child: RaisedButton(
              shape: StadiumBorder(),
              elevation: 0,
              highlightElevation: 0,
              textColor: Colors.white,
              color: Cos.zhutise,
              onPressed: () {
                _savas();
                Navigator.of(context).pushAndRemoveUntil(
                    CupertinoPageRoute(builder: (context) => Home()),
                        (route) => route == null);
              },
              child: Text("注册"),
            ),
          ),
          const SizedBox(height: 10.0),
        ],
      ),
    );
  }



}